#include <hpdf.h>
#include <iostream>
#include <vector>
#include <codecvt>
#include <locale>
#include <cstdlib>  // for wcstombs
#include <cwchar>   // for wcslen
#include <clocale>  // for setlocale

// 定义每页的字符数和每行的字符数
const int CHARS_PER_PAGE = 300;
const int CHARS_PER_LINE = 15;

// 定义 Unicode 汉字范围（常用汉字）
const wchar_t START_CHAR = 0x4E00; // Unicode 起始字符
const wchar_t END_CHAR = 0x9FFF;   // Unicode 结束字符


void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data) {
    std::cerr << "ERROR: error_no=" << error_no << ", detail_no=" << detail_no << std::endl;
}

int main() {
    // 设置 locale 以支持宽字符
    std::locale::global(std::locale(""));
	
    // 创建 PDF 文档对象
    HPDF_Doc pdf = HPDF_New(error_handler, NULL);
    if (!pdf) {
        std::cerr << "Failed to create PDF document." << std::endl;
        return 1;
    }

    // 设置文档属性
    //HPDF_SetCompressionMode(pdf, HPDF_COMP_ALL);

    // 加载支持 Unicode 的字体文件（如 NotoSansCJK.ttf）
    const char *font_name = HPDF_LoadTTFontFromFile(pdf, "simsun.ttf", HPDF_TRUE);
    if (!font_name) {
        std::cerr << "Failed to load font." << std::endl;
        HPDF_Free(pdf);
        return 1;
    }

    // 获取字体对象
	HPDF_UseUTFEncodings(pdf);
    HPDF_SetCurrentEncoder(pdf, "UTF-8");
    HPDF_Font font = HPDF_GetFont(pdf, font_name, "UTF-8");

    // 开始生成字符
    wchar_t current_char = START_CHAR;
    int char_count = 0;
    int page_count = 0;
    while (current_char <= END_CHAR) {
        // 创建新页面
        HPDF_Page page = HPDF_AddPage(pdf);
        HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);

        // 设置字体和字体大小
        HPDF_Page_SetFontAndSize(page, font, 12);

        // 设置起始位置
        float x = 50;
        float y = HPDF_Page_GetHeight(page) - 50;

        // 写入字符
        for (int i = 0; i < CHARS_PER_PAGE; ++i) {
            if (current_char > END_CHAR) {
                break;
            }

wchar_t temp = current_char;
// 创建转换器
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;

// 将宽字符转换为多字节字符串
std::string multiByteStr = converter.to_bytes(temp);
const char* cString = multiByteStr.c_str();

    //std::cout << "转换后的字符串: " << multiByteStr << std::endl;
	
	
            // 将 Unicode 字符编码转换为 UTF-16 字符串
            /* wchar_t wchar = static_cast<wchar_t>(current_char);
            char buf[3];
            buf[0] = (wchar >> 8) & 0xFF;
            buf[1] = wchar & 0xFF;
            buf[2] = '\0'; */

            // 写入字符
            HPDF_Page_BeginText(page);
            HPDF_Page_TextOut(page, x, y, cString);
            HPDF_Page_EndText(page);

            // 更新位置
            x += 20; // 假设每个字符宽度为20
            if ((i + 1) % CHARS_PER_LINE == 0) {
                x = 50;
                y -= 20; // 假设每行高度为20
            }
			

            // 更新字符
            current_char++;
            char_count++;
        }

        page_count++;
    }

    // 保存 PDF 文件
    if (HPDF_SaveToFile(pdf, "output.pdf") != HPDF_OK) {
        std::cerr << "Failed to save PDF file." << std::endl;
        HPDF_Free(pdf);
        return 1;
    }

    // 释放资源
    HPDF_Free(pdf);

    std::cout << "PDF generated successfully with " << page_count << " pages and " << char_count << " characters." << std::endl;
    return 0;
}
